package com.wjjun.base.binding.adapter

import android.view.ViewGroup
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.*
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.google.android.flexbox.FlexboxLayoutManager
import com.wjjun.base.ui.viewmodel.BaseItemViewModel
import com.wjjun.base.utils.ItemDragCallback
import com.wjjun.base.utils.ObArrayList
import com.wjjun.base.widget.decoration.FloatItemDecoration
import com.wjjun.base.widget.decoration.ViewModelItemDecoration

/**
 *
 * author : wangjunjun
 * date   : 2022/6/28
 * desc   :
 */
class RecyclerViewAdapter {
    companion object {

        @JvmStatic
        @BindingAdapter("itemAnimator")
        fun setAnimator(recyclerView: RecyclerView, itemAnimator: RecyclerView.ItemAnimator?) {
            recyclerView.itemAnimator = itemAnimator
        }

        @JvmStatic
        @BindingAdapter(value = ["spanSizeLookup"])
        fun setSpanSizeLookup(recyclerView: RecyclerView, spanSizeLookup: SpanSizeLookup?) {
            if (recyclerView.layoutManager is GridLayoutManager) {
                val gridLayoutManager = recyclerView.layoutManager as GridLayoutManager?
                if (spanSizeLookup != null) {
                    gridLayoutManager!!.spanSizeLookup = spanSizeLookup
                }
            }
        }

        @JvmStatic
        @BindingAdapter(value = ["spanCount"])
        fun setSpanCount(recyclerView: RecyclerView, spanCount: Int) {
            if (spanCount <= 0) {
                return
            }
            if (recyclerView.layoutManager is GridLayoutManager) {
                val gridLayoutManager = recyclerView.layoutManager as GridLayoutManager?
                gridLayoutManager!!.spanCount = spanCount
            } else if (recyclerView.layoutManager is StaggeredGridLayoutManager) {
                (recyclerView.layoutManager as StaggeredGridLayoutManager?)!!.spanCount = spanCount
            }
        }

        @JvmStatic
        @BindingAdapter("orientation")
        fun setOrientation(recyclerView: RecyclerView, orientation: Int) {
            if (recyclerView.layoutManager is GridLayoutManager) {
                (recyclerView.layoutManager as GridLayoutManager?)!!.orientation = orientation
            } else if (recyclerView.layoutManager is LinearLayoutManager) {
                (recyclerView.layoutManager as LinearLayoutManager?)!!.orientation = orientation
            } else if (recyclerView.layoutManager is StaggeredGridLayoutManager) {
                (recyclerView.layoutManager as StaggeredGridLayoutManager?)!!.orientation =
                    orientation
            }
        }

        @JvmStatic
        @BindingAdapter("flexDirection")
        fun setFlexDirection(recyclerView: RecyclerView, flexDirection: Int) {
            if (recyclerView.layoutManager is FlexboxLayoutManager) {
                (recyclerView.layoutManager as FlexboxLayoutManager).flexDirection = flexDirection
            }
        }

        @JvmStatic
        @BindingAdapter("alignItems")
        fun setAlignItems(recyclerView: RecyclerView, alignItems: Int) {
            if (recyclerView.layoutManager is FlexboxLayoutManager) {
                (recyclerView.layoutManager as FlexboxLayoutManager).alignItems = alignItems
            }
        }

        @JvmStatic
        @BindingAdapter("flexWrap")
        fun setFlexWrap(recyclerView: RecyclerView, flexWrap: Int) {
            if (recyclerView.layoutManager is FlexboxLayoutManager) {
                (recyclerView.layoutManager as FlexboxLayoutManager).flexWrap = flexWrap
            }
        }

        @JvmStatic
        @BindingAdapter("justifyContent")
        fun setJustifyContent(recyclerView: RecyclerView, justifyContent: Int) {
            if (recyclerView.layoutManager is FlexboxLayoutManager) {
                (recyclerView.layoutManager as FlexboxLayoutManager).justifyContent = justifyContent
            }
        }

        @JvmStatic
        @BindingAdapter("addDefDecoration")
        fun setViewDivider(recyclerView: RecyclerView, boolean: Boolean) {
            if (boolean) {
                val dividerDecoration: ItemDecoration =
                    ViewModelItemDecoration(recyclerView.context)
                recyclerView.addItemDecoration(dividerDecoration)
            }
        }

        @JvmStatic
        @BindingAdapter("floatView")
        fun setFloatView(recyclerView: RecyclerView, view: ViewGroup?) {
            if (view != null) {
                val dividerDecoration = FloatItemDecoration(recyclerView.context)
                dividerDecoration.setFloatContent(view)
                recyclerView.addItemDecoration(dividerDecoration)
            }
        }


        @JvmStatic
        @BindingAdapter(value = ["drag", "dragList"])
        fun setDrag(recyclerView: RecyclerView, drag: Boolean, list: ObArrayList<BaseItemViewModel<*>>?) {
            if (drag && list != null) {
                val itemTouchHelper = ItemTouchHelper(ItemDragCallback(list))
                itemTouchHelper.attachToRecyclerView(recyclerView)
            }
        }
    }
}